<?php
 
include_once("../configure/configure.php");						//--> global var
include_once("../admin/tools/smarty/libs/Smarty.class.php");		//--> out template
include_once("constant/GlobalConstant.php");

include_once("impl/UserImpl.php");
include_once("utils/ValidationUtils.php");						//--> validation


include_once("../includeSmarty.php");
include_once("../includeTag.php");
include_once("../includeCategories.php");



if(isset($_POST['Submit'])){	
	$userImpl 		= new UserImpl(DB_TAG_SYSTEM);									//--> user
	$validUtils		= new ValidationUtils();									//--> Validation
	
	$password		= $_POST['password'];
	$rpassword 		= $_POST['rpassword'];
	$email 			= $_POST['email'];
	
	$status 		= 'normal';
	
	if(empty($email))
		$error_message = 'Email is required.';
	else if(!$validUtils->isEmail($email, "empty"))
		$error_message = 'Email format is invalid.';	
	else if(empty($password))
		$error_message = 'Password is required.';
	else if(empty($rpassword))
		$error_message = 'Retype-password is required.';
	else if($password != $rpassword){
		$error_message = 'Password do not match.';
	}else if($userImpl->IsExistByEmail($name)){
		$error_message = 'Login email be existed.';
	}else{
		$obj = new UserDomain();
				
		$obj->setName('');
		$obj->setGroupId(6);
		$obj->setPassword($password);
		$obj->setEmail($email);
		$obj->setStatus($status);
		
		
		$isReturn = $userImpl->add($obj);
		if($isReturn){			
			if(!is_object($oCategory)){
				$location = "admin.php";
				
				header("Location: $location");
				exit;
			}
		}else
			$error_message = 'System error, register fail.';
	}
}

$smarty->assign('oTags', $oTags);
$smarty->assign('oCatTree', $oCatTree);

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL_HTTP);

$smarty->assign('message', $error_message);
$smarty->assign('name', $name);
$smarty->assign('password', $password);
$smarty->assign('rpassword', $rpassword);
$smarty->assign('email', $email);


$smarty->display('createAccount.html');
?>
